//
//  PublicBuyNumberView.m
//  Demo
//
//  Created by MacPro on 15/12/9.
//  Copyright © 2015年 pccb. All rights reserved.
//

#import "PublicBuyNumberView.h"

@interface PublicBuyNumberView()
{
    UILabel *_buyNumberLabel;
}

@end

@implementation PublicBuyNumberView

- (instancetype)initPublicBuyNumberViewWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        self.backgroundColor = HexRGB(0xd7d7d7);
        
        UIView *containerView = [[UIView alloc] init];
        containerView.backgroundColor = [UIColor whiteColor];
        containerView.frame = CGRectMake(40.0f, 20.0f, self.width - 80.0f, self.height - 40.0f);
        containerView.layer.borderWidth = 1.0f;
        containerView.layer.borderColor = HexRGB(0x7e7e7e).CGColor;
        [self addSubview:containerView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15.0f, 10.0f, 30.0f, 30.0f)];
        label.text = @"购买";
        label.font = Default_Font_14;
        label.textColor = HexRGB(0x6c6c6c);
        label.textAlignment = NSTextAlignmentRight;
        [containerView addSubview:label];
        
        
        // 1.购买数量
        _buyNumberLabel = [[UILabel alloc] init];
        _buyNumberLabel.text     = [NSString stringWithFormat:@"1"];
        _buyNumberLabel.font     = Default_Font_14;
        _buyNumberLabel.textColor = HexRGB(0x7d7d7d);
        _buyNumberLabel.frame           = CGRectMake(label.right + 5.0f, label.top, self.width / 3.0f - 10.0f, 30.0f);
        _buyNumberLabel.textAlignment   = NSTextAlignmentCenter;
        _buyNumberLabel.layer.borderColor = HexRGB(0xc3c3c3).CGColor;
        _buyNumberLabel.layer.borderWidth = 1.0f;
        [containerView addSubview:_buyNumberLabel];
        
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(_buyNumberLabel.right + 5.0f, 10.0f, 35.0f, 30.0f)];
        label2.text = @"份";
        label2.font = Default_Font_14;
        label2.textColor = HexRGB(0x6c6c6c);
        label2.textAlignment = NSTextAlignmentLeft;
        [containerView addSubview:label2];
        
        
        //  +选择
        UIButton * buttonPrev       =  [UIButton buttonWithType:UIButtonTypeCustom];
        buttonPrev.frame = CGRectMake(_buyNumberLabel.left, _buyNumberLabel.top, 30.0f, 30.0f);
        [buttonPrev setTitle:@"+" forState:UIControlStateNormal];
        [buttonPrev setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [buttonPrev setTitleColor:HexRGB(0x7d7d7d) forState:UIControlStateHighlighted];
        [buttonPrev addTarget:self action:@selector(showPreviousNumber:) forControlEvents:UIControlEventTouchUpInside];
        buttonPrev.titleLabel.font          = Default_Font_15;
        buttonPrev.backgroundColor = HexRGB(0x9b9b9b);
        [containerView addSubview:buttonPrev];
        
        UIButton * buttonNext          = [UIButton buttonWithType:UIButtonTypeCustom];
        buttonNext.frame = CGRectMake(_buyNumberLabel.right - 30.0f , _buyNumberLabel.top, 30.0f, 30.f);
        [buttonNext setTitle:@"-" forState:UIControlStateNormal];
        [buttonNext setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [buttonNext setTitleColor:HexRGB(0x7d7d7d) forState:UIControlStateHighlighted];
        [buttonNext addTarget:self action:@selector(showNextNumber:) forControlEvents:UIControlEventTouchUpInside];
        buttonNext.titleLabel.font = Default_Font_15;
        buttonNext.backgroundColor = HexRGB(0x9b9b9b);
        [containerView addSubview:buttonNext];
        
        CGFloat buttonWidth = containerView.width /4.0f;
        
        for (int i = 1; i < 3; i++) {
            UIButton * submitBtn          = [UIButton buttonWithType:UIButtonTypeCustom];
            submitBtn.frame = CGRectMake(containerView.width - i * (buttonWidth + 10.0f), containerView.height - 35.0f, buttonWidth, 25.f);
            
            [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [submitBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
            submitBtn.titleLabel.font = Default_Font_14;
            [containerView addSubview:submitBtn];
            
            if (i == 1) {
                [submitBtn setTitle:@"确认" forState:UIControlStateNormal];
                submitBtn.backgroundColor = DefaultColor_NavigationBar;
                [submitBtn addTarget:self action:@selector(clickSubmitBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            }else {
                [submitBtn setTitle:@"取消" forState:UIControlStateNormal];
                submitBtn.backgroundColor = HexRGB(0x9b9b9b);
                [submitBtn addTarget:self action:@selector(clickCancelBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            }
            
            
            
        }
        
        
    }
    
    return self;

}

- (void)clickCancelBtnAction:(UIButton *)sender
{
    [self removeFromSuperview];
}

- (void)showPreviousNumber:(UIButton *)sender
{
    NSInteger number = [_buyNumberLabel.text integerValue];
    number ++;
    
    _buyNumberLabel.text = [NSString stringWithFormat:@"%ld",number];
}

- (void)showNextNumber:(UIButton *)sender
{
    NSInteger number = [_buyNumberLabel.text integerValue];
    number --;
    if (number <= 0) {
        number = 0;
    }
    
    _buyNumberLabel.text = [NSString stringWithFormat:@"%ld",number];
}

- (void)clickSubmitBtnAction:(UIButton *)sender
{
    if (self.SubmitBuyNumberBlock) {
        NSInteger number = [_buyNumberLabel.text integerValue];
        self.SubmitBuyNumberBlock(number);
        _buyNumberLabel.text = @"1";
    }
    
    //取消弹出框
    [self clickCancelBtnAction:nil];
}

@end
